package com.qtummatrix.controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qtummatrix.entity.RoleEntity;
import com.qtummatrix.service.AuthotityManagerService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by Gongshiyong  2019-11-26 13:58
 */
@Controller("UserRoleAction")
public class AddUserRoleController extends ActionSupport implements ModelDriven<RoleEntity> {

    @Autowired
    private AuthotityManagerService authotityManagerService;

    private RoleEntity roleEntity = new RoleEntity();

    private String rolePermission;

    public void setRolePermission(String rolePermission) {
        this.rolePermission = rolePermission;
    }

    private String bankname;

    public String getBankname() {
        return bankname;
    }

    public void setBankname(String bankname) {
        this.bankname = bankname;
    }

    public String getRolePermission() {
        return rolePermission;
    }

    private Map<String, Object> info = new HashMap<String, Object>();

    public Map<String, Object> getInfo() {
        return info;
    }

    public RoleEntity getModel() {
        return roleEntity;
    }


    /**
     * 获取所有角色 -- 分页显示
     *
     * @return
     */
    public String getRoleList() {
        HttpServletRequest request = ServletActionContext.getRequest();
        //当前页码
        int pageNo = Integer.parseInt(request.getParameter("pageNo")) - 1;
        //页面的记录条数
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //用户名
        String rooname = roleEntity.getRooname();
        //所属银行
        String bankname = getBankname();

        //查询使用者信息
        List<Map<String, Object>> roleList = authotityManagerService.getRoleList(rooname, bankname, pageNo, pageSize);
        //获取使用者的记录数
        Integer roleSize = authotityManagerService.getRoleSize(rooname, bankname);
        info.put("list", roleList);
        // 返回的数据列表
        // 当前页数
        info.put("pageNum", pageNo + 1);
        // 总页数
        int pages = roleSize % pageSize == 0 ? roleSize / pageSize : roleSize / pageSize + 1;
        info.put("pages", pages);
        // 总条数
        info.put("total", roleSize);
        return SUCCESS;
    }

    //删除角色
    public String deleteRoleById() {
        int id = roleEntity.getId();
        int i = authotityManagerService.deleteRoleById(id);
        if (i > 0) {
            info.put("deleteResult", 1);
        } else {
            info.put("deleteResult", 0);
        }
        return SUCCESS;
    }

    //添加角色信息
    public String addRoleInfo() {
        String rolename = roleEntity.getRooname();
        int bankid = roleEntity.getBankid();
        String description = roleEntity.getDescription();
        String rolePermission = getRolePermission();

        int i = authotityManagerService.addRole(rolename, bankid, description, rolePermission);
        if (i > 0) {
            info.put("addRoleResult", "角色添加成功");
            return SUCCESS;
        } else {
            info.put("addRoleResult", "角色添加失败");
            return ERROR;
        }
    }

    //校验添加的角色名
    public String getValidateRoleName() {
        String rooname = roleEntity.getRooname();
        int i = authotityManagerService.getValidateRoleName(rooname);
        if (i == 1) {
            info.put("validateRoleName", "可用");
        } else {
            info.put("validateRoleName", "不可用");
        }
        return SUCCESS;
    }
}
